package logic

import (
	"community/communityrpc/communityrpc"
	"community/communityrpc/internal/svc"
	"community/models/mysqlinit"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *communityrpc.AddRequest) (*communityrpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	var post mysqlinit.Post

	mysqlinit.DB.Debug().Where("id = ?", in.PostId).Find(&post)

	if post.ID == 0 {
		return nil, fmt.Errorf("没有此帖子，请重新输入")
	}

	comment := mysqlinit.Comment{

		UserId:   in.UserId,
		PostId:   in.PostId,
		CommText: in.CommText,
	}
	mysqlinit.DB.Debug().Create(&comment)
	return &communityrpc.AddResponse{
		Id: int64(comment.ID),
	}, nil
}
